function loadImage (url) {
    return new Promise((r, j) => {
        let image = new Image()
        image.src = url
        image.onload = () => r(image)
    })
}

function getFixedImageSize (image, maxWidth, maxHeight) {
    let imgWidth = image.width
    let imgHeight = image.height
    let widthFixed = imgHeight > 0 ? (imgWidth / imgHeight * maxHeight) : 0
    let heightFixed = imgWidth > 0 ? (maxWidth * imgHeight / imgWidth) : 0

    if (heightFixed <= maxHeight) {
        return {width: maxWidth, height: Math.round(heightFixed)}
    }
    if (widthFixed <= maxWidth) {
        return {width: Math.round(widthFixed), height: maxHeight}
    }
}

module.exports = {
    loadImage,
    getFixedImageSize
}
